package com.xywy.tangandroid.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.provider.SyncStateContract.Constants;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xywy.tangandroid.R;
import com.xywy.tangandroid.base.BaseActivity;
import com.xywy.tangandroid.bean.User;
import com.xywy.tangandroid.util.Constant;
import com.xywy.tangandroid.util.MD5;
import com.xywy.tangandroid.vollery.OnVolleyResponseListener;
import com.xywy.tangandroid.vollery.PostRequest;
import com.xywy.tangandroid.vollery.VolleyManager;

/**
 * 消息设置
 */
public class MessageSettingActivity extends BaseActivity {

	protected static final int FAIL = 0;
	private TextView middle_title;
	private RelativeLayout left_img;

	private Button iv_question_online; // 在线消息的开关
	private Button iv_new_question; // 新信息的开关

	boolean is_receive_online_message; // 是否打开了CheckBox
	boolean is_receive_new_message;

	private SharedPreferences sp;
	private Editor editor;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.activity_msg_setting);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("消息设置");
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		iv_question_online = (Button) findViewById(R.id.iv_question_online);
		iv_new_question = (Button) findViewById(R.id.iv_new_question);

		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		editor = sp.edit();

	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);

		iv_question_online.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				switch (message_state) {
				case 0: // 1 open
				case 2: // 3
					crotorlMessage("glu_msg_box_0", "open");
					break;
				case 1: // 0 close
				case 3: // 2 close
					crotorlMessage("glu_msg_box_0", "close");
					break;

				default:
					break;
				}
			}
		});
		iv_new_question.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				switch (message_state) {
				case 0: // 2 open
				case 1: // 3
					crotorlMessage("glu_msg_box_1", "open");
					break;
				case 2: // 0 close
				case 3: // 1 close
					crotorlMessage("glu_msg_box_1", "close");
					break;

				default:
					break;
				}
			}
		});
	}

	@Override
	protected void initData() {
		getMessageState();

	}

	/**
	 * 获取当前页面状态； 0，都为选中 1，单独选中在线 2，单独选中系统 3，全部选中
	 */
	private int message_state;

	private void getMessageState() {
		message_state = sp.getInt("message_state", 0);
		switch (message_state) {
		case 0:
			iv_question_online
					.setBackgroundResource(R.drawable.toggle_close_button);
			iv_new_question
					.setBackgroundResource(R.drawable.toggle_close_button);
			break;
		case 1:
			iv_question_online
					.setBackgroundResource(R.drawable.toggle_open_button);
			iv_new_question
					.setBackgroundResource(R.drawable.toggle_close_button);
			break;
		case 2:
			iv_question_online
					.setBackgroundResource(R.drawable.toggle_close_button);
			iv_new_question
					.setBackgroundResource(R.drawable.toggle_open_button);
			break;
		case 3:
			iv_question_online
					.setBackgroundResource(R.drawable.toggle_open_button);
			iv_new_question
					.setBackgroundResource(R.drawable.toggle_open_button);
			break;

		}
	}

	/**
	 * 保存状态
	 * 
	 * @param state
	 */
	private void saveMessageState(int state) {
		editor.putInt("message_state", state).commit();

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			MessageSettingActivity.this.finish();
			break;

		default:
			break;
		}
	}

	/**
	 * 
	 */
	private void crotorlMessage(final String setting_key,
			final String setting_value) {
		String sign = null;
		try {
			sign = MD5.md5s(Constant.Sign + Constant.tag);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		String url = "http://api.yun.xywy.com/index.php/server/api/setting/post/";
		PostRequest<String> postRequest = new PostRequest<String>(url,
				String.class, new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						showToast("设置失败");
					}

					@Override
					public void onSuccess(String response) {
						try {
							JSONObject jsonObject = new JSONObject(response);
							if (jsonObject.getInt("state") == 200) {

								if (setting_key.equals("glu_msg_box_0")) {
									switch (message_state) {
									case 0:
										saveMessageState(1);
										break;
									case 1:
										saveMessageState(0);
										break;
									case 2:
										saveMessageState(3);
										break;
									case 3:
										saveMessageState(2);
										break;

									default:
										break;
									}
									getMessageState();
								} else {
									switch (message_state) {
									case 0:
										saveMessageState(2);
										break;
									case 1:
										saveMessageState(3);
										break;
									case 2:
										saveMessageState(0);
										break;
									case 3:
										saveMessageState(1);
										break;

									default:
										break;
									}
									getMessageState();
								}

							} else {
								showToast("设置失败");
							}

						} catch (JSONException e) {
							showToast("设置失败");
							e.printStackTrace();
						}

					}
				});

		Map<String, String> params = new HashMap<String, String>();
		params.put("sign", sign);
		params.put("tag", Constant.tag);
		params.put("xywy_userid", User.getIUserInstance(context).getUserid());
		params.put("setting_key", setting_key);
		params.put("setting_value", setting_value);

		postRequest.setParams(params);
		postRequest.setIsParseJson(false);
		VolleyManager.addRequest(postRequest, this);
	}

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case FAIL:

				break;

			default:
				break;
			}
		};
	};

}
